package com.alibaba.org.service.impl;

import com.alibaba.basic.util.PageList;
import com.alibaba.org.domain.Department;
import com.alibaba.org.mapper.DepartmentMapper;
import com.alibaba.org.query.DepartmentQuery;
import com.alibaba.org.service.IDepartmentService;
import com.alibaba.basic.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 业务实现类：
 */
@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Resource
    private DepartmentMapper departmentMapper;


    @Override
    public void add(Department department) {
        //处理dirPath
        //1.没有选上级部门
        //Department( manager_id=327, manager=null, parent_id=null, parent=null, children=[])
        //2.选了上级部门
        //Department( manager_id=327, manager=null, parent_id=4, parent=null, children=[])
        //3.选了之后叉掉
        //Department( manager_id=326, manager=null, parent_id=null, parent=null, children=[])
        //添加之后会返回一个自增长id
        departmentMapper.save(department);

        //添加的时候对dirPath的处理
        String dirPath = "";
        if(department.getParentId() == null){//没有选择上级部门 or //选了之后×掉了
            //获取自增id
            Long id  = department.getId();
            dirPath = "/" + id;
        }else{
            //获取上级部门
            Long parent_id = department.getParentId();
            //获取上级部门的dirPath
            String parentDirPath = departmentMapper.loadById(parent_id).getDirPath();
            dirPath = parentDirPath + "/" + department.getId();
        }

        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }

    @Override
    public void update(Department department) {
        //添加的时候对dirPath的处理
        String dirPath = "";
        if(department.getParentId() == null){//没有选择上级部门 or //选了之后×掉了
            Long id  = department.getId();
            dirPath = "/" + id;
        }else{
            //获取上级部门
            Long parent_id = department.getParentId();
            //获取上级部门的dirPath
            String parentDirPath = departmentMapper.loadById(parent_id).getDirPath();
            dirPath = parentDirPath + "/" + department.getId();
        }
        //设置新的dirPath
        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }





    @Override
    public List<Department> getDeptTree() {
        //用集合获取 所有部门
        List<Department> departments = departmentMapper.loadAll();
        Map<Long,Department> map = new HashMap<>();
        //将每一个部门通过循环装到map 里  键为部门id(唯一性) 值(每一个部门对象)
        //iter for循环快捷方式
        for (Department department : departments) {
            map.put(department.getId(),department);
        }
        //获取前端需要得到的数据(部门树)
        List<Department> deptTree = new ArrayList<>();
        for (Department department : departments) {
            if (department.getParentId()==null){
                deptTree.add(department);
            }else{
                Long parentId = department.getParentId();
                Department parentDepartment = map.get(parentId);
                parentDepartment.getChildren().add(department);
            }
        }

        return deptTree;
    }
}
